package com.xjh.mange.common.config;

import com.xjh.mange.common.enums.FinishStatusEnum;
import com.xjh.mange.model.Thing;
import com.xjh.mange.service.ThingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 徐佳辉
 * @date 2020/12/27 21:53
 */
@Slf4j
@Component
@EnableScheduling
public class TimeTask {

    @Autowired
    private ThingService thingService;


    /**
     * 每天半夜12点让所有事件未完成
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void everyDayThingToNoFinish() {
        log.info("定时任务执行开始执行 : [{}]", new Date());
        List<Thing> list = thingService.list();
        for (Thing thing : list) {
            thing.setState(FinishStatusEnum.NOT_FINISH.getCode());
        }
        thingService.updateBatchById(list);
        log.info("定时任务执行结束 : [{}]", new Date());
    }
}
